package com.gangquan360.smartadmin.module.order.saleorder.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.gangquan360.smartadmin.common.domain.PageResultDTO;
import com.gangquan360.smartadmin.common.domain.ResponseDTO;
import com.gangquan360.smartadmin.module.order.saleorder.dao.SaleOrderDao;
import com.gangquan360.smartadmin.module.order.saleorder.domain.dto.SaleOrderDTO;
import com.gangquan360.smartadmin.module.order.saleorder.domain.dto.SaleOrderQueryDTO;
import com.gangquan360.smartadmin.module.order.saleorder.domain.entity.SaleOrderEntity;
import com.gangquan360.smartadmin.util.SmartPaginationUtil;
import com.gangquan360.smartadmin.util.SmartBeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [ 销售订单表 ]
 *
 * @author wiggin
 * @version 1.0
 * @company wiggin
 * @copyright (c) 2019 wigginInc. All rights reserved.
 * @date 2020-03-31 19:43:21
 * @since JDK1.8
 */
@Service
public class SaleOrderService {

    @Autowired
    private SaleOrderDao saleOrderDao;


    /**
     * @author wiggin
     * @description 分页查询
     * @date 2020-03-31 19:43:21
     */
    public ResponseDTO<PageResultDTO<SaleOrderDTO>> queryByPage(SaleOrderQueryDTO queryDTO) {
        Page page = SmartPaginationUtil.convert2PageQueryInfo(queryDTO);
        List<SaleOrderEntity> entities = saleOrderDao.queryByPage(page, queryDTO);
        List<SaleOrderDTO> dtoList = SmartBeanUtil.copyList(entities, SaleOrderDTO.class);
        page.setRecords(dtoList);
        PageResultDTO<SaleOrderDTO> pageResultDTO = SmartPaginationUtil.convert2PageResultDTO(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * @author wiggin
     * @description 添加
     * @date 2020-03-31 19:43:21
     */
    public ResponseDTO<String> add(SaleOrderDTO addDTO) {
        SaleOrderEntity entity = SmartBeanUtil.copy(addDTO, SaleOrderEntity.class);
        saleOrderDao.insert(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 编辑
     * @date 2020-03-31 19:43:21
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(SaleOrderDTO updateDTO) {
        SaleOrderEntity entity = SmartBeanUtil.copy(updateDTO, SaleOrderEntity.class);
        saleOrderDao.updateById(entity);
        return ResponseDTO.succ();
    }


    /**
     * @author wiggin
     * @description 删除
     * @date 2020-03-31 19:43:21
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long id) {
        saleOrderDao.deleteById(id);
        return ResponseDTO.succ();
    }

    /**
     * @author wiggin
     * @description  根据ID查询
     * @date 2020-03-31 19:43:21
    */
    public ResponseDTO<SaleOrderDTO> detail(Long id){
        SaleOrderEntity entity = saleOrderDao.selectById(id);
        SaleOrderDTO dto =  SmartBeanUtil.copy(entity,SaleOrderDTO.class);
        return ResponseDTO.succData(dto);
    }
}
